package com.kye.express.business.waybill_list.subfragment.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.databinding.LayoutLogisticsListItemBinding;
import com.kye.model.bean.logistics.LogisticsItemBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 17/01/2018
 * @updated : 17/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class WaybillListItemViewBinder extends ItemViewBinder<LogisticsItemBean, WaybillListItemViewBinder.ViewHolder> {

    private OnItemListener onItemListener;

    public WaybillListItemViewBinder(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.layout_logistics_list_item, parent, false);
        return new WaybillListItemViewBinder.ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull LogisticsItemBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private LogisticsItemBean item;
        private LayoutLogisticsListItemBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.container)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onItemClicked(item);
                            }
                        }
                    });

            RxView.clicks(binding.urge)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onItemUrgeClicked(item);
                            }
                        }
                    });

            RxView.clicks(binding.score)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onItemScoreClicked(item);
                            }
                        }
                    });
        }

        public void bindData(LogisticsItemBean item) {
            this.item = item;
            binding.setData(item);
            binding.urge.setVisibility(View.GONE);
            binding.urgeDone.setVisibility(View.GONE);
            binding.score.setVisibility(View.GONE);
            binding.scoreStarts.setVisibility(View.GONE);
            switch (item.getType()) {
                case 0: {
                    binding.companySendReceive.setText(TextUtils.isEmpty(item.getSendCompanyCode()) ? "- -" : item.getSendCompanyCode());
                    binding.personSendReceive.setText(TextUtils.isEmpty(item.getSender()) ? "- -" : item.getSender());
                    break;
                }
                case 1: {
                    binding.companySendReceive.setText(TextUtils.isEmpty(item.getReceiptCompanyCode()) ? "- -" : item.getReceiptCompanyCode());
                    binding.personSendReceive.setText(TextUtils.isEmpty(item.getRecipient()) ? "- -" : item.getRecipient());
                    break;
                }
            }
            if (item.getQueryState() == 0) {
                /**
                 * 未签收
                 */
                /**
                 * urge：    检查催件0=已派送催件，不可重复催件 1=可以派送催件 2 = 运单已签收，不可催件
                 * rush：    催件   （0=未到预派时间，不可催件 1=可以派送催件 2=运单已签收，不可催件 3=10分钟内不可重复催件）
                 * pushMark: 催件标识(0=未到预派时间，不可催件 1=可以派送催件 2=运单已签收，不可催件 3=10分钟内不可重复催件)
                 */
                int rush = item.getRush();
                switch (rush) {
                    case 0:
                        break;
                    case 1:
                        binding.urge.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        break;
                    case 3:
                        binding.urgeDone.setVisibility(View.VISIBLE);
                        break;
                    default:
                        break;
                }
            } else {
                /**
                 * 已签收
                 */

                /**
                 * todo : 24小时内展示服务评分操作, 是否可以评分
                 ratingStatus
                     可评分（code=1）
                     已评分（code=2）
                     已过期（code=3）
                     没权限（code=4）
                     不能评（code=5）
                 */
                String ratingStatus = item.getRatingStatus();
                switch (ratingStatus) {
                    case "1": {
                        binding.score.setVisibility(View.VISIBLE);
                        break;
                    }
                    case "2": {
                        binding.score.setVisibility(View.GONE);
                        binding.scoreStarts.setVisibility(View.VISIBLE);
                        /**
                         * 显示评分
                         */
                        int score = 0;
                        try {
                            score = Integer.parseInt(item.getScore());
                            score = 6 - score;
                        } catch (NumberFormatException e) {
                            Logger.d("scroe parse error");
                        }
                        binding.scoreStarts.setRating(score);
                        break;
                    }
                    case "3":
                    case "4":
                    case "5": {
                        binding.score.setVisibility(View.GONE);
                        break;
                    }
                    default: {
                        binding.score.setVisibility(View.GONE);
                        break;
                    }
                }
            }
        }

    }

    public interface OnItemListener {
        void onItemClicked(LogisticsItemBean item);
        void onItemUrgeClicked(LogisticsItemBean item);
        void onItemScoreClicked(LogisticsItemBean item);
    }
}
